package com.ocom.payconfig.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityRechargeRule;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfRechargeRule;
import com.ocom.payconfig.service.XfRechargeRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class XfRechargeRuleServiceImpl extends ServiceImpl<IDaoXfRechargeRule, EntityRechargeRule> implements XfRechargeRuleService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
	public List<EntityRechargeRule> getRechargeRule(Long companyId,Integer subAccount) {
		QueryWrapper<EntityRechargeRule> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id", companyId);
		queryWrapper.eq("sub_account",subAccount);
		Integer count = this.count(queryWrapper);
		if (count == 0) {
			EntityRechargeRule entityRechargeRule = new EntityRechargeRule();
			entityRechargeRule.setComId(companyId);
			entityRechargeRule.setRuleNo(1);
			entityRechargeRule.setRecharge(50000);
			entityRechargeRule.setDMode(0);
			entityRechargeRule.setDValue(0);
			entityRechargeRule.setSubAccount(subAccount);
			this.save(entityRechargeRule);
		}
		queryWrapper.orderByAsc("rule_no");
		return this.list(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
	public Boolean setRechargeRule(Long companyId, EntityRechargeRule entityRechargeRule) {
		UpdateWrapper<EntityRechargeRule> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("com_id", entityRechargeRule.getComId());
		updateWrapper.eq("rule_no", entityRechargeRule.getRuleNo());
		updateWrapper.eq("sub_account",entityRechargeRule.getSubAccount());
		updateWrapper.set("recharge", entityRechargeRule.getRecharge());
		updateWrapper.set("d_mode", entityRechargeRule.getDMode());
		updateWrapper.set("d_value", entityRechargeRule.getDValue());
		return this.saveOrUpdate(entityRechargeRule, updateWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
	public Boolean deleteRechargeRule(Long companyId, EntityRechargeRule entityRechargeRule) {
		if (entityRechargeRule.getRuleNo() == 1) {
			UpdateWrapper<EntityRechargeRule> updateWrapper = new UpdateWrapper<>();
			updateWrapper.eq("com_id", entityRechargeRule.getComId());
			updateWrapper.eq("rule_no", entityRechargeRule.getRuleNo());
			updateWrapper.eq("sub_account",entityRechargeRule.getSubAccount());
			updateWrapper.set("recharge", 0);
			updateWrapper.set("d_mode", 0);
			updateWrapper.set("d_value", 0);
			return this.update(entityRechargeRule, updateWrapper);
		} else {
			QueryWrapper<EntityRechargeRule> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("com_id", entityRechargeRule.getComId());
			queryWrapper.eq("rule_no", entityRechargeRule.getRuleNo());
			queryWrapper.eq("sub_account",entityRechargeRule.getSubAccount());
			return this.remove(queryWrapper);
		}
	}
}
